package com.bj58.ecdata.dao.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesHelper
{
  private Properties pro = null;
  
  public PropertiesHelper(String path)
    throws Exception
  {
    this.pro = loadProperty(path);
  }
  
  public PropertiesHelper(InputStream inputStream)
  {
    this.pro = new Properties();
    try
    {
      this.pro.load(inputStream);
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
  
  public String getString(String key)
  {
      return this.pro.getProperty(key);
  }
  
  public String getString(String key,String defaultVal)
  {
	  if(pro.containsKey(key)){
		  return this.pro.getProperty(key);
	  }
	  return defaultVal;
  }
  
  
  public int getInt(String key) throws Exception
  {
    try
    {
      return Integer.parseInt(this.pro.getProperty(key));
    }
    catch (Exception e)
    {
      throw new Exception("key:" + key);
    }
  }
  
  public int getInt(String key,int defaultVal)
  {
	  if(pro.containsKey(key)){
		  return Integer.parseInt(this.pro.getProperty(key));
	  }
	  return defaultVal;
  }
  
  public double getDouble(String key)
    throws Exception
  {
    try
    {
      return Double.parseDouble(this.pro.getProperty(key));
    }
    catch (Exception e)
    {
      throw new Exception("key:" + key);
    }
  }
  
  public double getDouble(String key,double defaultVal)
  {
	  if(pro.containsKey(key)){
		  return Double.parseDouble(this.pro.getProperty(key));
	  }
	  return defaultVal;
  }
  
  public long getLong(String key)
    throws Exception
  {
    try
    {
      return Long.parseLong(this.pro.getProperty(key));
    }
    catch (Exception e)
    {
      throw new Exception("key:" + key);
    }
  }
  
  public long getLong(String key,long defaultVal)
  {
	  if(pro.containsKey(key)){
		  return Long.parseLong(this.pro.getProperty(key));
	  }
	  return defaultVal;
  }
  
  public float getFloat(String key)
    throws Exception
  {
    try
    {
      return Float.parseFloat(this.pro.getProperty(key));
    }
    catch (Exception e)
    {
      throw new Exception("key:" + key);
    }
  }
  
  public boolean getBoolean(String key)
    throws Exception
  {
    try
    {
      return Boolean.parseBoolean(this.pro.getProperty(key));
    }
    catch (Exception e)
    {
      throw new Exception("key:" + key);
    }
  }
  
  public boolean getBoolean(String key,boolean defaultVal)
  {
	  if(pro.containsKey(key)){
		  return Boolean.parseBoolean(this.pro.getProperty(key));
	  }
	  return defaultVal;
  }
  
  
  public Set<Object> getAllKey()
  {
    return this.pro.keySet();
  }
  
  public Collection<Object> getAllValue()
  {
    return this.pro.values();
  }
  
  public Map<String, Object> getAllKeyValue()
  {
    Map<String, Object> mapAll = new HashMap<String, Object>();
    Set<Object> keys = getAllKey();
    
    Iterator<Object> it = keys.iterator();
    while (it.hasNext())
    {
      String key = it.next().toString();
      mapAll.put(key, this.pro.get(key));
    }
    return mapAll;
  }

  private Properties loadProperty(String filePath)
      throws Exception
  {
      FileInputStream fin = null;
      Properties pro =new Properties();
      try
      {
          fin = new FileInputStream(filePath);
          pro.load(fin);
      }
      catch(IOException e)
      {
          throw e;
      }
      if(fin != null)
          fin.close();
      return pro;
  }

}
